package com.eblacorp.archive.service.impl;

import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import com.eblacorp.archive.annotations.FnTransactional;
import com.eblacorp.archive.dao.fn.DrawingDocumentDao;
import com.eblacorp.archive.domain.fn.DrawingDocument;
import com.eblacorp.archive.exceptions.NoAttachedFnDocument;
import com.eblacorp.archive.exceptions.NotFnDocumentException;
import com.eblacorp.archive.exceptions.NullValueException;
import com.eblacorp.archive.service.DrawingDocumentService;
import com.filenet.api.constants.CheckinType;

public class DrawingDocumentServiceImpl implements DrawingDocumentService {

	private DrawingDocumentDao drawingDocumentDao;

	@Override
	@FnTransactional
	public List<DrawingDocument> search(DrawingDocument drawingDocument,
			Integer startPage, Integer pageSize, boolean withDocument)
			throws IllegalArgumentException, NotFnDocumentException,
			IntrospectionException, IllegalAccessException,
			InvocationTargetException, InstantiationException {
		return drawingDocumentDao.getByExample(drawingDocument, null, null,
				withDocument);
	}

	@Override
	@FnTransactional
	public DrawingDocument addNewDocument(DrawingDocument drawingDocument,
			File file) throws IllegalArgumentException, NotFnDocumentException,
			IntrospectionException, IllegalAccessException,
			InvocationTargetException, InstantiationException,
			NullValueException, IOException {
		DrawingDocument newDrawingDocument = null;
		drawingDocument.setDocumentTitle(drawingDocument
				.getRealtyRegistrationNumber());
		if (file == null) {
			newDrawingDocument = drawingDocumentDao.create(drawingDocument, file,
					CheckinType.MINOR_VERSION);
		} else {
			newDrawingDocument = drawingDocumentDao.create(drawingDocument, file,
					CheckinType.MAJOR_VERSION);
		}
		return newDrawingDocument;
	}
	
	@Override
	@FnTransactional
	public DrawingDocument updateDocument(DrawingDocument drawingDocument,
			File file) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException, IOException {
		drawingDocument.setDocumentTitle(drawingDocument.getRealtyRegistrationNumber());
		if(file!=null){
			drawingDocument = drawingDocumentDao.update(drawingDocument, file, CheckinType.MAJOR_VERSION);
		}else{
			drawingDocument = drawingDocumentDao.update(drawingDocument, null, null);
		}
		return drawingDocument;
	}
	
	@Override
	@FnTransactional
	public void deleteDocument(DrawingDocument drawingDocument, boolean allVersion) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException {
		drawingDocumentDao.delete(drawingDocument, allVersion);
	}

	@Override
	public String getViewerUrl(DrawingDocument drawingDocument) {
		return drawingDocumentDao.getWcmJavaViewerUrl(drawingDocument);
	}
	
	@Override
	@FnTransactional
	public byte[] download(DrawingDocument drawingDocument) throws NoAttachedFnDocument, IOException {
		
		return drawingDocumentDao.download(drawingDocument);
	}
	public DrawingDocumentDao getDrawingDocumentDao() {
		return drawingDocumentDao;
	}

	public void setDrawingDocumentDao(DrawingDocumentDao drawingDocumentDao) {
		this.drawingDocumentDao = drawingDocumentDao;
	}

	

}
